package bb.lanxing.adapter.mine;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import bb.lanxing.R;
import bb.lanxing.util.SportTypeResUtil;

public class SportTypeAdapter extends RecyclerView.Adapter<SportTypeAdapter.ViewHolder> {
    private List<Integer> sportTypeList;
    private int selectedSportType;

    private OnItemClickListener listener;
    private Context mContext;

    public SportTypeAdapter(List<Integer> sportTypeList, int selectedSportType) {
        this.sportTypeList = sportTypeList;
        this.selectedSportType = selectedSportType;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_sport_type, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.icon.setImageResource(SportTypeResUtil.getSportIcon(sportTypeList.get(position)));
        if (sportTypeList.get(position) == selectedSportType) {
            holder.icon.setImageResource(SportTypeResUtil.getSelectedSportIcon(sportTypeList.get(position)));
        }
        holder.name.setText(SportTypeResUtil.getTypeTextRes(sportTypeList.get(position)));
        holder.name.setEnabled(sportTypeList.get(position) == selectedSportType);
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(position);
                selectedSportType = position;
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return sportTypeList.size();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView icon;
        TextView name;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.icon);
            name = itemView.findViewById(R.id.name);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}